package top.heue.epro.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import java.io.File;

import top.heue.epro.bean.ConfigBean;

public class PermissionUtil {

    //权限检查
    public static boolean checkDrawPermission(Context context) {
        return Settings.canDrawOverlays(context);
    }

    public static boolean storagePermissionCheck(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);
            return true;
        }

        return false;
    }

    //第一次启动
    public static void isFirstRun(Context context) {
        //LOG.i("目录", ConfigUtil.getConfigPath(context));
        File file = new File(ConfigUtil.getConfigPath(context));
        if (!file.exists()) {
            //第一次
            //LOG.i("第一次启动应用");
            try {
                int i1, i2;
                String s1;
                i1 = 10;
                s1 = "#FAF9DE";
                i2 = 50;
                ConfigBean b = new ConfigBean(i1, s1, i2);
                ConfigUtil.writeUtil(context, b);
            } catch (Exception e) {
                //OG.i("第一次启动初始化异常", e);
            }
        }
    }

    //弹窗提示
    /*
    public static void tipUserByPopu(Activity context) {
        @SuppressLint("InflateParams") View contentView =LayoutInflater.from(context).inflate(
            R.layout.popu_tip, null);
        PopupWindow popupWindow = new PopupWindow(contentView,   LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOutsideTouchable(false);
        //popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_alpha));
        popupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        popupWindow.showAtLocation(context.getWindow().getDecorView(),Gravity.CENTER,0,0);
    }
    */
    //权限申请
    public static void gotoDrawPermit(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        context.startActivity(intent);
    }

    /*
     private boolean isGrantExternalRW(Activity activity) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
     Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
     activity.requestPermissions(new String[]{
     Manifest.permission.READ_EXTERNAL_STORAGE,
     Manifest.permission.WRITE_EXTERNAL_STORAGE
     }, 1);
     return false;
     }

     return true;
     }
     */

    //打开设置
    public static void simpleSetting(Context context) {
        Intent intent = new Intent();
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 将用户引导到系统设置页面
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            context.startActivity(intent);
        } catch (Exception e) {//抛出异常就直接打开设置
            intent = new Intent(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
        }
    }
}
